﻿
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLKS2014.PUBLIC;
using QLKS2014.BLL;

namespace QLKS2014.GUI.StupidWindow
{
    /// <summary>
    /// Mô tả: theo quy định 6
    /// thay đổi số lượng loại khách
    /// thany đổi số khách tối đa
    /// thay đổi tỉ lệ phụ thu
    /// </summary>
    /// 
    public partial class SettingsWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string _PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(_PropertyName));
        }
        ObservableCollection<LoaiPhongPublic> _ocLoaiPhong;
        ObservableCollection<LoaiKhachHangPublic> _ocLoaiKhachHang;

        public ObservableCollection<LoaiKhachHangPublic> OcLoaiKhachHang
        {
            get { return _ocLoaiKhachHang; }
            set
            {
                _ocLoaiKhachHang = value;
                OnPropertyChanged("OcLoaiKhachHang");
            }
        }

        public ObservableCollection<LoaiPhongPublic> OcLoaiPhong
        {
            get { return _ocLoaiPhong; }
            set
            {
                _ocLoaiPhong = value;
                OnPropertyChanged("OcLoaiPhong");
            }
        }


        //DataSetting dataSetting = new DataSetting();

        public SettingsWindow()
        {
            InitializeComponent();
            Loaded += SettingWindow_Loaded;
        }

        void SettingWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // reload data whenever window reload
            _initialize();
        }

        private void _initialize()
        {
            // init ocloaiphong
            List<LoaiPhongPublic> lLoaiPhong = new LoaiPhongBll().selectAll();
            if (lLoaiPhong != null)
                OcLoaiPhong = new ObservableCollection<LoaiPhongPublic>(lLoaiPhong);
            else
                OcLoaiPhong = new ObservableCollection<LoaiPhongPublic>();

            // init ocLoaiKhachHang
            List<LoaiKhachHangPublic> lLoaiKhach = new LoaiKhachHangBll().selectAll();
            if (lLoaiKhach != null)
                OcLoaiKhachHang = new ObservableCollection<LoaiKhachHangPublic>(lLoaiKhach);
            else
                OcLoaiKhachHang = new ObservableCollection<LoaiKhachHangPublic>();
        }

        
        

        

        /// <summary>
        /// xoa loai khach
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listTypeRoom_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Delete)
            //{
            //    ViewDataSettingTypeRoom item
            //        = (ViewDataSettingTypeRoom)listTypeRoom.SelectedItem;
            //    dataSetting.dataTypeRoom.deleteTypeRoom(item.idTypeRoom);
            //    listTypeRoom.Items.RemoveAt(listTypeRoom.SelectedIndex);
            //    dataSetting.viewDataTypeRoom(ref this.listTypeRoom);
            //}
        }

        private void listTypeCustomer_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Delete)
            //{
            //    ViewDataSettingTypeCustomer item
            //        = (ViewDataSettingTypeCustomer)listTypeCustomer.SelectedItem;
            //    dataSetting.dataCustomer.deleteTypeCustomer(item.id);
            //    listTypeRoom.Items.RemoveAt(listTypeCustomer.SelectedIndex);
            //    dataSetting.viewDataTypeCustomer(ref this.listTypeCustomer);
            //}
        }

        private void btnEditPassWord_Click(object sender, RoutedEventArgs e)
        {
            //if (string.IsNullOrEmpty(this.tbxUserName.Text) ||
            //    string.IsNullOrEmpty(this.pwbOldPassWord.Password) ||
            //    string.IsNullOrEmpty(this.pwbNewPassWord.Password) ||
            //    string.IsNullOrEmpty(this.pwbNewPassWord_Copy.Password))
            //{
            //    MessageBox.Show("Nhập đầy đủ thông tin");
            //    return;
            //}
            //if (!this.pwbNewPassWord.Password.Equals(
            //    this.pwbNewPassWord_Copy.Password)
            //    )
            //{
            //    MessageBox.Show("Xác nhận lại mật khẩu");
            //    return;
            //}
            //DataUser.editPassWord(
            //    this.pwbOldPassWord.Password,
            //    this.pwbNewPassWord.Password, this.tbxUserName.Text
            //    );
        }

        private void GroupBox_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Enter)
            //{
            //    if (string.IsNullOrEmpty(this.tbxMaxCustomer.Text) &&
            //    string.IsNullOrEmpty(this.tbxNameNewTypeRoom.Text) &&
            //    string.IsNullOrEmpty(this.tbxPrice.Text))
            //        return;
            //    dataSetting.dataTypeRoom.testExist(this.tbxNameNewTypeRoom.Text,
            //        decimal.Parse(this.tbxPrice.Text),
            //        int.Parse(this.tbxMaxCustomer.Text));
            //    dataSetting.viewDataTypeRoom(ref this.listTypeRoom);
            //}
        }

        private void GroupBox_KeyDown_1(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Enter)
            //{
            //    if (string.IsNullOrEmpty(this.tbxNameTypeCustomer.Text) &&
            //    string.IsNullOrEmpty(this.tbxRateSurcharge.Text))
            //        return;
            //    dataSetting.dataCustomer.testExist(
            //        this.tbxNameTypeCustomer.Text,
            //        float.Parse(this.tbxRateSurcharge.Text)
            //        );
            //    dataSetting.viewDataTypeCustomer(ref this.listTypeCustomer);
            //}
        }

        private void GroupBox_KeyDown_2(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Enter)
            //{
            //    if (string.IsNullOrEmpty(this.tbxUserName.Text) ||
            //    string.IsNullOrEmpty(this.pwbOldPassWord.Password) ||
            //    string.IsNullOrEmpty(this.pwbNewPassWord.Password) ||
            //    string.IsNullOrEmpty(this.pwbNewPassWord_Copy.Password))
            //    {
            //        MessageBox.Show("Nhập đầy đủ thông tin");
            //        return;
            //    }
            //    if (!this.pwbNewPassWord.Password.Equals(
            //        this.pwbNewPassWord_Copy.Password)
            //        )
            //    {
            //        MessageBox.Show("Xác nhận lại mật khẩu");
            //        return;
            //    }
            //    DataUser.editPassWord(
            //        this.pwbOldPassWord.Password,
            //        this.pwbNewPassWord.Password, this.tbxUserName.Text
            //        );
            //}
        }

        private void gb_RoomTypeSetting_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void txtbPrice_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("^[0-9]+");
            bool match = regex.IsMatch(e.Text);
            e.Handled = !match;
        }

        private void txtbCustomerMax_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("^[0-9]+");
            bool match1 = regex.IsMatch(e.Text);
            TextBox textbox = sender as TextBox;
            String newText = textbox.Text + e.Text;
            int num = int.Parse(newText);
            bool match2 = num > 0;
            e.Handled = !(match1 && match2);
        }

        private void btnAddingRoomType_Click(object sender, RoutedEventArgs e)
        {
            _addNewRoomType();
        }

        private void _addNewRoomType()
        {
            if (_checkNewRoomTypeInput())
            {
                LoaiPhongPublic loaiPhong0 = new LoaiPhongPublic();
                loaiPhong0.TenLoaiPhong = txtbRoomTypeName.Text;
                loaiPhong0.DonGia = int.Parse(txtbPrice.Text);
                loaiPhong0.SoKhachToiDa = int.Parse(txtbCustomerMax.Text);
                new LoaiPhongBll().insert(loaiPhong0);
                _initialize();
            }
            else
            {
                MessageBox.Show("Mời nhập đầy đủ thông tin!");
            }


        }

        private bool _checkNewRoomTypeInput()
        {
            return !(String.IsNullOrEmpty(txtbRoomTypeName.Text) || String.IsNullOrEmpty(txtbPrice.Text) || String.IsNullOrEmpty(txtbCustomerMax.Text));
        }

        private void dgRoomType_KeyDown(object sender, KeyEventArgs e)
        {
            if ((Keyboard.Modifiers == ModifierKeys.Shift) && e.Key == Key.Delete)
            {
                MessageBoxResult result = MessageBox.Show("Bạn chắc chắn muốn xóa chứ?", "Nhắc Nhở", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (result == MessageBoxResult.Yes)
                {
                    DataGrid dgData = sender as DataGrid;
                    _deleteRoomTypeDataGridRows(dgData);
                    _initialize(); // reload datagrid
                }
                return;
            }
        }

        private void _deleteRoomTypeDataGridRows(DataGrid dgData)
        {
            LoaiPhongPublic loaiPhong0 = dgData.SelectedItem as LoaiPhongPublic;
            if (loaiPhong0 != null)
                new LoaiPhongBll().delete(loaiPhong0);
        }

        private void dgRoomType_LoadingRow(object sender, DataGridRowEventArgs e)
        {

        }

        private void gb_CustomerTypeSettings_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void txtbSurchargeRate_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("^[0-9.]+");
            bool match = regex.IsMatch(e.Text);
            if(!match)
                e.Handled = true;
            String newText = txtbSurchargeRate.Text + e.Text;
            try
            {
                float test = float.Parse(newText);
                e.Handled = false;
            }
            catch (Exception ex)
            {
                e.Handled = true;
            }

        }

        private void btnAddingCustomerType_Click(object sender, RoutedEventArgs e)
        {
            if (_checkNewCustomerTypeInput())
            {
                LoaiKhachHangPublic loaiKhachHang0 = new LoaiKhachHangPublic();
                loaiKhachHang0.TenLoaiKhachHang = txtbCustomerTypeName.Text;
                loaiKhachHang0.TyLePhuThu = int.Parse(txtbSurchargeRate.Text);
                new LoaiKhachHangBll().insert(loaiKhachHang0);
                _initialize();
            }
            else
            {
                MessageBox.Show("Mời nhập đầy đủ thông tin!");
            }
        }

        private bool _checkNewCustomerTypeInput()
        {
            return !(String.IsNullOrEmpty(txtbCustomerTypeName.Text)||String.IsNullOrEmpty(txtbSurchargeRate.Text));
        }

        private void dgCustomerType_KeyDown(object sender, KeyEventArgs e)
        {
            if ((Keyboard.Modifiers == ModifierKeys.Shift) && e.Key == Key.Delete)
            {
                MessageBoxResult result = MessageBox.Show("Bạn chắc chắn muốn xóa chứ?", "Nhắc Nhở", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (result == MessageBoxResult.Yes)
                {
                    DataGrid dgData = sender as DataGrid;
                    _deleteCustomerTypeDataGridRows(dgData);
                    _initialize(); // reload datagrid
                }
                return;
            }
        }

        private void _deleteCustomerTypeDataGridRows(DataGrid dgData)
        {
            LoaiKhachHangPublic loaiKhach0 = dgData.SelectedItem as LoaiKhachHangPublic;
            if (loaiKhach0 != null)
                new LoaiKhachHangBll().delete(loaiKhach0);
        }

        private void dgCustomerType_LoadingRow(object sender, DataGridRowEventArgs e)
        {

        }

        private void gb_AccountSettings_KeyDown(object sender, KeyEventArgs e)
        {

        }

        // result value
        //-1. input empty
        // 0. repassword is false
        // 1. ok
        // 2. ten nguoi dung k ton tai
        private void btnChangePassWord_Click(object sender, RoutedEventArgs e)
        {
            int result = _checkChangingPasswordInput();
            switch (result)
	        {
                case -1:
                    MessageBox.Show("Mời nhập đầy đủ thông tin!");
                    return;
                case 0:
                    MessageBox.Show("Nhập lại password không khớp!");
                    return;
                case 1:
                    break;
                case 2:
                    MessageBox.Show("Tên người dùng không tồn tại!");
                    return;
		        default:
                return;
	        }

            NguoiDungPublic nguoiDung0 = new NguoiDungPublic();
            nguoiDung0.TenNguoiDung = txtbUserName.Text;
            nguoiDung0 = new NguoiDungBll().selectByName(nguoiDung0)[0];
            nguoiDung0.MatKhau = pwbNewPassword.Password;
            new NguoiDungBll().update(nguoiDung0);

            // Notify successful
            MessageBox.Show("Đổi mật khẩu thành công!");

            pwbOldPassword.Password = "";
            pwbNewPassword.Password = "";
            pwbReNewPassword.Password = "";
            return;

            
        }

        //return
        //-1. input empty
        // 0. repassword is false
        // 1. ok
        // 2. ten nguoi dung k ton tai
        private int _checkChangingPasswordInput()
        {
            if (String.IsNullOrEmpty(txtbUserName.Text) || String.IsNullOrEmpty(pwbOldPassword.Password) || String.IsNullOrEmpty(pwbNewPassword.Password) || String.IsNullOrEmpty(pwbReNewPassword.Password))
                return -1;
            if (!pwbNewPassword.Password.Equals(pwbReNewPassword.Password))
                return 0;
            if (!_checkUserName(txtbUserName.Text))
                return 2;
            return 1;
        }

        private bool _checkUserName(string userName_)
        {
            NguoiDungPublic nguoiDung0 = new NguoiDungPublic();
            nguoiDung0.TenNguoiDung = userName_;
            var lNguoiDung = new NguoiDungBll().selectByName(nguoiDung0);
            if(lNguoiDung==null||lNguoiDung.Count==0)
                return false;
            return true;
        }

        private void txtbRoomTypeName_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            String newText = ((TextBox)sender).Text + e.Text;
            Regex regex = new Regex("^[a-zA-Z_0-9]{1,30}$");
            bool match = regex.IsMatch(newText);
            e.Handled = !match;
        }
    }
}